<?php

App::uses('AppController', 'Controller');

/**
 * PostsController Controller
 * 
 * @author oanhnn
 * @property Post $Post
 */
class PostsController extends AppController {

    /**
     * Components
     * @var array
     */
    public $components = array('Captcha');

    /**
     * Helpers
     * @var array
     */
    public $helpers = array('Time');

    /**
     * Uses Models
     * @var array
     */
    public $uses = array('Post', 'Commnet');

    /**
     * process before do action
     * @return void
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }

    /**
     * process after action and before render view
     * @return void
     */
    public function beforeRender() {
        parent::beforeRender();
    }

    public function index() {
        $conditions = array();
        if (!$this->Auth->loggedIn()) {
            $conditions['is_published'] = 1;
        }
        $limit = 5;
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => $limit,
            'order' => array(
                'Post.publish_time' => 'DESC',
                'Post.modified' => 'DESC',
                'Post.created' => 'DESC'
            )
        );
        $this->set('posts', $this->paginate('Post'));
    }

    public function view($slug = null) {
        if (!$slug) {
            $this->Session->setFlash(__('Invalid post id.'), FMSG_ERROR);
            $this->redirect(array('action' => 'latest'));
        }
        $post = $this->Post->find('first', array('conditions' => array('slug' => $slug)));
        if (empty($post)) {
            throw new NotFoundException("Post doesn't exist");
        }
        $this->set(compact('post'));
    }

    public function add() {
        if (!empty($this->request->data)) {
            // modified data
            $data = $this->request->data;

            $this->Post->create();
            if ($this->Post->save($data)) {
                $this->Session->setFlash(__('Post created.'), FMSG_SUCCESS);
                $post = $this->Post->read();
                $this->redirect(array('action' => 'view', $post['Post']['slug']));
            } else {
                $this->Session->setFlash(__('Post failed.'), FMSG_ERROR);
            }
        }
    }

    public function edit($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid post id.'), FMSG_ERROR);
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->request->data)) {
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('Post updated.'), FMSG_SUCCESS);
                $post = $this->Post->read();
                $this->redirect(array('action' => 'view', $post['Post']['slug']));
            } else {
                $this->Session->setFlash(__('Post failed.'), FMSG_ERROR);
            }
        } else {
            $this->data = $this->Post->read(null, $id);
        }
    }

    public function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid post id.'), FMSG_ERROR);
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Post->delete($id)) {
            $this->Session->setFlash(__('Post deleted.'), FMSG_SUCCESS);
        } else {
            $this->Session->setFlash(__('Post did not delete.'), FMSG_ERROR);
        }
        $this->redirect(array('action' => 'index'));
    }

    public function latest($id = null) {
        
    }

}
